<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Perform an aggregation using the aggregation framework</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongocollection.html">MongoCollection</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongocollection.batchinsert.html">MongoCollection::batchInsert</a></div>
 <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongocollection.aggregate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoCollection::aggregate</h1>
  <p class="verinfo">(PECL mongo &gt;=1.3.0)</p><p class="refpurpose"><span class="refname">MongoCollection::aggregate</span> &mdash; <span class="dc-title">Perform an aggregation using the aggregation framework</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongocollection.aggregate-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>MongoCollection::aggregate</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$pipeline</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$op</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   The MongoDB
   <a href="http://docs.mongodb.org/manual/core/aggregation-pipeline/" class="link external">&raquo;&nbsp;aggregation framework</a>
   provides a means to calculate aggregated values without having to use
   MapReduce. While MapReduce is powerful, it is often more difficult than
   necessary for many simple aggregation tasks, such as totaling or averaging
   field values.
  </p>
  <p class="para">
   This method accepts either a variable amount of pipeline operators, or a
   single array of operators constituting the pipeline.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongocollection.aggregate-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">pipeline</code></em></dt>

    <dd>

     <p class="para">
      An array of pipeline operators, or just the first operator.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">op</code></em></dt>

    <dd>

     <p class="para">
      The second pipeline operator.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">...</code></em></dt>

    <dd>

     <p class="para">
      Additional pipeline operators.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongocollection.aggregate-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The result of the aggregation as an array. The <var class="varname"><var class="varname">ok</var></var> will
   be set to <em>1</em> on success, <em>0</em> on failure.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongocollection.aggregate-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   When an error occurs an array with the following keys will be returned:
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      <var class="varname"><var class="varname">errmsg</var></var> - containing the reason for the failure
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <var class="varname"><var class="varname">code</var></var> - the errorcode of the failure
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <var class="varname"><var class="varname">ok</var></var> - will be set to 0.
     </span>
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongocollection.aggregate-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="mongocollection.aggregate.example.basic">
   <p><strong>Example #1 <span class="methodname"><strong>MongoCollection::aggregate()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    The following example aggregation operation pivots data to create a set of
    author names grouped by tags applied to an article. Call the aggregation
    framework by issuing the following command:
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Mongo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectDB</span><span style="color: #007700">(</span><span style="color: #DD0000">"examples"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"article"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'title'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'this&nbsp;is&nbsp;my&nbsp;title'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'author'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'posted'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'pageViews'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'tags'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(&nbsp;</span><span style="color: #DD0000">'fun'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'good'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'fun'&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'comments'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'author'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'joe'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'text'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'this&nbsp;is&nbsp;cool'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'author'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'sam'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'text'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'this&nbsp;is&nbsp;bad'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'other'&nbsp;</span><span style="color: #007700">=&gt;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;),<br />);<br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"w"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$ops&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$project'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"author"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"tags"&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'$unwind'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$tags'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$group'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"tags"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$tags'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"authors"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'$addToSet'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$author'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;),<br />);<br /></span><span style="color: #0000BB">$results&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">aggregate</span><span style="color: #007700">(</span><span style="color: #0000BB">$ops</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$results</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;result&quot;]=&gt;
  array(2) {
    [0]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      array(1) {
        [&quot;tags&quot;]=&gt;
        string(4) &quot;good&quot;
      }
      [&quot;authors&quot;]=&gt;
      array(1) {
        [0]=&gt;
        string(3) &quot;bob&quot;
      }
    }
    [1]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      array(1) {
        [&quot;tags&quot;]=&gt;
        string(3) &quot;fun&quot;
      }
      [&quot;authors&quot;]=&gt;
      array(1) {
        [0]=&gt;
        string(3) &quot;bob&quot;
      }
    }
  }
  [&quot;ok&quot;]=&gt;
  float(1)
}
</pre></div>
   </div>
  </div>

  <p class="para">
   The following examples use the <a href="http://media.mongodb.org/zips.json" class="link external">&raquo;&nbsp;zipcode data set</a>.
   Use mongoimport to load this data set into your mongod instance.
  </p>

  <div class="example" id="mongocollection.aggregate.example.zipcode.population">
   <p><strong>Example #2 <span class="methodname"><strong>MongoCollection::aggregate()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    To return all states with a population greater than 10 million, use the following aggregation operation:
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Mongo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectDB</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"zips"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">aggregate</span><span style="color: #007700">(array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$group'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$state'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'totalPop'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'$sum'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$pop'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$match'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'totalPop'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'$gte'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">*</span><span style="color: #0000BB">1000</span><span style="color: #007700">*</span><span style="color: #0000BB">1000</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;result&quot;]=&gt;
  array(7) {
    [0]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;TX&quot;
      [&quot;totalPop&quot;]=&gt;
      int(16986510)
    }
    [1]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;PA&quot;
      [&quot;totalPop&quot;]=&gt;
      int(11881643)
    }
    [2]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;NY&quot;
      [&quot;totalPop&quot;]=&gt;
      int(17990455)
    }
    [3]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;IL&quot;
      [&quot;totalPop&quot;]=&gt;
      int(11430602)
    }
    [4]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;CA&quot;
      [&quot;totalPop&quot;]=&gt;
      int(29760021)
    }
    [5]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;OH&quot;
      [&quot;totalPop&quot;]=&gt;
      int(10847115)
    }
    [6]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;FL&quot;
      [&quot;totalPop&quot;]=&gt;
      int(12937926)
    }
  }
  [&quot;ok&quot;]=&gt;
  float(1)
}
</pre></div>
   </div>
  </div>

  <div class="example" id="mongocollection.aggregate.example.zipcode.population.average">
   <p><strong>Example #3 <span class="methodname"><strong>MongoCollection::aggregate()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    To return the average populations for cities in each state, use the following aggregation operation:
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Mongo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectDB</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"zips"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">aggregate</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$group'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'state'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$state'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'city'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$city'&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'pop'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'$sum'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$pop'&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$group'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$_id.state'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'avgCityPop'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'$avg'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'$pop'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;result&quot;]=&gt;
  array(51) {
    [0]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;DC&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(303450)
    }
    [1]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;DE&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(14481.913043478)
    }
    [2]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;RI&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(18933.283018868)
    }
    [3]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;AL&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(7907.2152641879)
    }
    [4]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;NH&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(5232.320754717)
    }
...
    [45]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;WY&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(3359.9111111111)
    }
    [46]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;MN&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(5335.4865853659)
    }
    [47]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;OK&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(6155.7436399217)
    }
    [48]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;IL&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(9931.0182450043)
    }
    [49]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;WI&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(7323.0074850299)
    }
    [50]=&gt;
    array(2) {
      [&quot;_id&quot;]=&gt;
      string(2) &quot;WV&quot;
      [&quot;avgCityPop&quot;]=&gt;
      float(2759.1953846154)
    }
  }
  [&quot;ok&quot;]=&gt;
  float(1)
}
</pre></div>
   </div>
  </div>

 </div>


 <div class="refsect1 seealso" id="refsect1-mongocollection.aggregate-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member">The MongoDB <a href="http://docs.mongodb.org/manual/core/aggregation-pipeline/" class="link external">&raquo;&nbsp;aggregation framework</a></li>
  </ul>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongocollection.html">MongoCollection</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongocollection.batchinsert.html">MongoCollection::batchInsert</a></div>
 <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
